package com.ruoyi.sl.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sl.domain.SlCheckUser;
import com.ruoyi.sl.mapper.SlCheckConfigMapper;
import com.ruoyi.sl.domain.SlCheckConfig;
import com.ruoyi.sl.service.ISlCheckConfigService;

/**
 * 复核配置Service业务层处理
 * 
 * @author admin
 * @date 2024-06-29
 */
@Service
public class SlCheckConfigServiceImpl implements ISlCheckConfigService 
{
    @Autowired
    private SlCheckConfigMapper slCheckConfigMapper;

    /**
     * 查询复核配置
     * 
     * @param checkcfgId 复核配置主键
     * @return 复核配置
     */
    @Override
    public SlCheckConfig selectSlCheckConfigByCheckcfgId(Long checkcfgId)
    {
        return slCheckConfigMapper.selectSlCheckConfigByCheckcfgId(checkcfgId);
    }

    /**
     * 查询复核配置列表
     * 
     * @param slCheckConfig 复核配置
     * @return 复核配置
     */
    @Override
    public List<SlCheckConfig> selectSlCheckConfigList(SlCheckConfig slCheckConfig)
    {
        return slCheckConfigMapper.selectSlCheckConfigList(slCheckConfig);
    }

    /**
     * 新增复核配置
     * 
     * @param slCheckConfig 复核配置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSlCheckConfig(SlCheckConfig slCheckConfig)
    {
        slCheckConfig.setCreateTime(DateUtils.getNowDate());
        int rows = slCheckConfigMapper.insertSlCheckConfig(slCheckConfig);
        insertSlCheckUser(slCheckConfig);
        return rows;
    }

    /**
     * 修改复核配置
     * 
     * @param slCheckConfig 复核配置
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSlCheckConfig(SlCheckConfig slCheckConfig)
    {
        slCheckConfig.setUpdateTime(DateUtils.getNowDate());
        slCheckConfigMapper.deleteSlCheckUserByCheckcfgId(slCheckConfig.getCheckcfgId());
        insertSlCheckUser(slCheckConfig);
        return slCheckConfigMapper.updateSlCheckConfig(slCheckConfig);
    }

    /**
     * 批量删除复核配置
     * 
     * @param checkcfgIds 需要删除的复核配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlCheckConfigByCheckcfgIds(Long[] checkcfgIds)
    {
        slCheckConfigMapper.deleteSlCheckUserByCheckcfgIds(checkcfgIds);
        return slCheckConfigMapper.deleteSlCheckConfigByCheckcfgIds(checkcfgIds);
    }

    /**
     * 删除复核配置信息
     * 
     * @param checkcfgId 复核配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSlCheckConfigByCheckcfgId(Long checkcfgId)
    {
        slCheckConfigMapper.deleteSlCheckUserByCheckcfgId(checkcfgId);
        return slCheckConfigMapper.deleteSlCheckConfigByCheckcfgId(checkcfgId);
    }

    /**
     * 新增复核人信息
     * 
     * @param slCheckConfig 复核配置对象
     */
    public void insertSlCheckUser(SlCheckConfig slCheckConfig)
    {
        List<SlCheckUser> slCheckUserList = slCheckConfig.getSlCheckUserList();
        Long checkcfgId = slCheckConfig.getCheckcfgId();
        if (StringUtils.isNotNull(slCheckUserList))
        {
            List<SlCheckUser> list = new ArrayList<SlCheckUser>();
            for (SlCheckUser slCheckUser : slCheckUserList)
            {
                slCheckUser.setCheckcfgId(checkcfgId);
                list.add(slCheckUser);
            }
            if (list.size() > 0)
            {
                slCheckConfigMapper.batchSlCheckUser(list);
            }
        }
    }
}
